<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class main extends MY_Controller {
    /* 来自父类
     * public $ceeconfigArray;//cee配置
     * public $userinfoArray; //用户信息
     * public $isloginArray; // 用户是否登录
     */
    //析构
    function __construct(){
        parent::__construct();
        //加载数据库
        $this->load->model('Main_model','_model');
    }
    /* ---------------------------------------------精选商品------------------------------------------ */
	public function index(){
	    //商品信息
	    $goodsArray = $this->_model->getGoodsArray();//数组，不止有0：商品，还有1：日期
	    //获取商品评论
	    $commentArray = $this->_model->getCommentArray($goodsArray[1]);
	    //获取虚拟头像
	    $virtualUserArray = $this->_model->getVirtualUserArray(sizeof($goodsArray[0]),$goodsArray[1]);
	    //丰富商品信息
	    $goodsArray_true = $this->goodsChange($goodsArray[0],$commentArray,$virtualUserArray);
	    //底部导言
	    $bottomword = $this->_model->bottomword();
	    //底部链接
	    $bottomlinks = $this->_model->bottomlinks();
	    //是否展示转盘
	    $isshowzhanpan = 0;
	    /* if (isset($_GET['source']) && $_GET['source']=='fromnht') {
	    	if (!$this->session->userdata('fromnht')) {
	    		$isshowzhanpan = 1;
	    		$this->session->set_userdata('fromnht', 1);
	    	}
	    } */
	    //是否说说推荐的商品
	    $type = isset($_GET['type'])?$_GET['type']:'';
	    //"lingshi";"nvzhuang";"baobao";"xiezi";"peishi";"meizhuang";
	    $goodsid = isset($_GET['id'])?$_GET['id']:'';
	    if ($goodsid) {
	    	if(!empty($goodsid)) {
	    		$firstgoods = $this->_model->firstgoods($goodsid);
	    		$goodsArray_true2[] = $firstgoods;
	    		foreach ($goodsArray_true as $item){
	    			$goodsArray_true2[] = $item;
	    		}
	    		$goodsArray_true = $goodsArray_true2;
	    	}
	    }
	    //视图中的参数
	    $data = array(
	    	'appid'              => $this->ceeconfigArray['appid'],
	        'pf'                 => $this->session->userdata('pf')?$this->session->userdata('pf'):"qzone",
	   		'userinfo'           => $this->userinfoArray,
	    	'controller'         => 'main',
	    	'goodsArray'         => $goodsArray_true,
	    	'goodsArrayFromDate' => $goodsArray[1],
	    	'bottomword'         => $bottomword,
	    	'bottomlinks'        => $bottomlinks,
	    	'isshowzhanpan'      => $isshowzhanpan,
	    	'type'               => $type,
	    	'goodsid'             => $goodsid
	    );
	    $this->load->view('common/header',$data);
		$this->load->view('main');//加载视图view
		$this->load->view('common/footer');
	}
	//商品信息丰富
	function goodsChange($goodsArray,$commentArray,$virtualUserArray){
	    foreach ($goodsArray as $key=>$item){
	        //真实的评论
	        $goodsArray[$key]['commentArray'] = $this->_model->goodsTrueComment($item['id']);
	        //价格
	        $goodsArray[$key]['originalPrice'] = rtrim(rtrim($item['originalPrice'],'0'),'.');
	        $goodsArray[$key]['currentPrice']  = rtrim(rtrim($item['currentPrice'],'0'),'.');
	        //分享收藏
	        $_shared = $item['collected']*11;
	        $_collected=$item['shared']*11;
	        //分享收藏
	        switch(($key%15)+1){
	        	case 1: $_collected += 2659; $_shared += 1578; break;
	        	case 2: $_collected += 1865; $_shared += 1268; break;
	        	case 3: $_collected += 2187; $_shared += 1654; break;
	        	case 4: $_collected += 1356; $_shared += 987; break;
	        	case 5: $_collected += 2014; $_shared += 1475; break;
	        	case 6: $_collected += 1532; $_shared += 1021; break;
	        	case 7: $_collected += 3045; $_shared += 1128; break;
	        	case 8: $_collected += 1658; $_shared += 921; break;
	        	case 9: $_collected += 1058; $_shared += 754; break;
	        	case 10: $_collected += 2048; $_shared += 565; break;
	        	case 11: $_collected += 1077; $_shared += 808; break;
	        	case 12: $_collected += 1206; $_shared += 609; break;
	        	case 13: $_collected += 589; $_shared += 510; break;
	        	case 14: $_collected += 691; $_shared += 207; break;
	        	case 15: $_collected += 1503; $_shared += 1309; break;
	        }
	        $goodsArray[$key]['shared']=$_collected;
	        $goodsArray[$key]['collected']=$_shared;
	        //图标样式 css 女装、男装、鞋包、首饰、护肤、家居、其他
	        switch ($item['cateID']) {
	        	case 6:
	        	    $goodsArray[$key]['cateCss']="picture_lingshi";
	        	    $goodsArray[$key]['type']="lingshi";
	        	    break;
	        	case 1:
	        	    $goodsArray[$key]['cateCss']="picture_nvzhuang";
	        	    $goodsArray[$key]['type']="nvzhuang";
	        	    break;
	        	case 5:
	        	    $goodsArray[$key]['cateCss']="picture_baobao";
	        	    $goodsArray[$key]['type']="baobao";
	        	    break;
	        	case 2:
	        	    $goodsArray[$key]['cateCss']="picture_xiezi";
	        	    $goodsArray[$key]['type']="xiezi";
	        	    break;
	        	case 7:
	        	    $goodsArray[$key]['cateCss']="picture_peishi";
	        	    $goodsArray[$key]['type']="peishi";
	        	    break;
	        	case 4:
	        	    $goodsArray[$key]['cateCss']="picture_meizhuang";
	        	    $goodsArray[$key]['type']="meizhuang";
	        	    break;
	        	default:
	        	    $goodsArray[$key]['cateCss']="picture_icon1";
	        	    break;
	        }
	        $size = 4;
	        	
	        for($i=0;$i<$size;$i++){
	            //虚拟用户头像
	            $goodsArray[$key]['virtualuser'][] = $virtualUserArray[$key*4+$i]['pic'];
	            //虚拟用户评论
	            $goodsArray[$key]['comments'][]    = $commentArray[$key*4+$i]['comment'];
	        }
	    }
	    return $goodsArray;
	}
	//商品点击
	public function AjaxGoodsClick(){
		/* -----------参数为空--------- */
		if (empty($_GET['id'])) {
			echo json_encode(array('ret'=>-1));//错误
			return false;
		}
		$this->_model->AjaxGoodsClick($_GET['id']);
		echo $id = $_GET['id'];
		return false;
	}
	/* ---------------------------------------------一用户提交评论------------------------------------------ */
	public function AjaxCommentSubmit(){
	    $goodsid = $_GET['id']?$_GET['id']:false;
	    $comment = $_GET['comment']?$_GET['comment']:false;
	    $IP = $this->GetIP();
	    /* -----------参数为空--------- */
	    if (empty($goodsid) || empty($comment)) {
	    	echo json_encode(array('ret'=>-1));//错误
	    	return false;
	    }
	    /* -----------长度太长--------- */
	    $str_len = (int)($this->strlen_utf8($comment));
	    if ($str_len>25) {
	        echo json_encode(array('ret'=>-2));
	    	return false;
	    }
	    /* -----------是否包含地址、违规信息--------- */
	    $words = array('www','http','https','.net','。net','.com','。com','.cn','。cn','(兼|jian).+(职|zhi)?','[1-9][0-9]{4,}');
	    foreach ($words as $val){
	        if(preg_match("/$val/i", $comment)){
	            echo json_encode(array('ret'=>-3));
	    	    return false;
	        }
	    }
	    /* -----------是否在黑名单--------- */
	    $forbidOpenid = $this->_model->AjaxGetForbiddenId();
	    if ($forbidOpenid) {
	    	echo json_encode(array('ret'=>-4));
	    	return false;
	    }
	    /* -----------正常数据--------- */
	    //插入数据
	    $comment = $this->ceeclass->getwordfilter($comment);//接口过滤
	    $rs = $this->_model->AjaxCommentSubmit($goodsid,$comment,$_GET['img'],$IP);//($comment,$id,$IP,$img);
	    echo json_encode($rs);
	    return false;
		//var_dump($this->ceeclass->islogin());
	}
	/* ---------------------------------------------分享收藏------------------------------------------ */
	function AjaxCollectedShared(){
	    $id = !empty($_GET['id'])?$_GET['id']:false;
	    $action = $_GET['action']?$_GET['action']:1;
	    if (!$id) {//无数据 返回错误
	    	echo json_encode(array('ret'=>-1));
	    	return false;
	    }
	    //正常情况
	    $rs = $this->_model->AjaxCollectedShared($id,$action);
		echo json_encode($rs);
		return false;
	}
	/* --------------------------------------------- 留言 ------------------------------------------ */
	function AjaxCollectedLeaveMessage(){
		$userinfo = $this->userinfoArray;
		$message = $_POST['message']?$_POST['message']:'';
		if (!$message || $message=='有什么想对主编说的，建议，闲聊，唠嗑都可以哦~') {
			echo json_encode(array('ret'=>0,'msg'=>'提交失败，或者内容有误！'));
			return false;
		}
		//正常情况
	    $rs = $this->_model->AjaxCollectedLeaveMessage($message,$userinfo['tx']);
		echo json_encode($rs);
		return false;
	}
	/* ---------------------------------------------一些公用的方法------------------------------------------ */
	//字符长度
	function strlen_utf8($str) {
	    $i = 0;
	    $count = 0;
	    $len = strlen ($str);
	    while ($i < $len) {
	        $chr = ord ($str[$i]);
	        $count++;
	        $i++;
	        if($i >= $len) break;
	        if($chr & 0x80) {
	            $chr <<= 1;
	            while ($chr & 0x80) {
	                $i++;
	                $chr <<= 1;
	            }
	        }
	    }
	    return $count;
	}
}